# init database
# use manage.py shell to run
# better off use init_data.xml

from django.contrib.auth.models import User
from django.core.files import File
from core.models import Level, Member, Address
from core.views import extract_date
from shop.models import Category, DessertItem
from backend.models import Staff
from datetime import date
import os
import random

def create_level(name, discount):
    return Level.objects.create(name=name, discount_rate=discount)

def create_user(username, email, password):
    return User.objects.create_user(username=username, email=email, password=password)

def create_member(user, level, name, birthday, gender):
    today = date.today()
    return Member.objects.create(
        user = user, 
        level = level, 
        name = name, 
        birthday = birthday, 
        register_date = today,
        expire_date = today,
        gender = gender
    )

def create_address(member, country, state, city, street):
    return Address.objects.create(member=member, country=country, state=state, city=city, street=street)

def create_category(name):
    Category.objects.create(name=name)

def create_dessert(pic_path, **kwargs):
    pic = File(open(pic_path, 'rb'))
    di = DessertItem.objects.create(pic=pic, **kwargs)
    return di

def init_core():
    create_level('Platinum', 0.8)
    create_level('Golden', 0.85)
    create_level('Silver', 0.9)
    create_level('Bronze', 0.95)
    basic = create_level('Basic', 1.0)

    usernames = ('nana', 'jack', 'doddy')
    emails = ('nana@example.com', 'jack@example.com', 'doddy@example.com')
    passwords = ('123123', '123123', '123123')
    names = ('Nana Kim', 'Jack Ive', 'Doddy Smith')
    birthdays = ('1990-02-12', '1991-03-24', '1995-11-11')
    genders = ('F', 'M', 'O')
    countrys = ('China', 'England', 'U.S.')
    states = ('Jiangsu', 'London', 'California')
    cities = ('Nanjing', 'Tottehnam', 'San Fransisco')
    streets = ('22 Hankou Rd', '45 Hot Spurs Rd', '11 Google St.')
    genders = ('F', 'M', 'O')
    countries = ('China', 'England', 'U.S.')
    states = ('Jiangsu', 'London', 'California')
    cities = ('Nanjing', 'Tottehnam', 'San Fransisco')
    streets = ('22 Hankou Rd', '45 Hot Spurs Rd', '11 Google St.')

    for i in range(len(usernames)):
        user = create_user(usernames[i], emails[i], passwords[i])
        print 'created user %s' % user
        birthday = extract_date(birthdays[i])
        member = create_member(user, basic, names[i], birthday, genders[i])
        print 'created member %s' % member
        address = create_address(member, countries[i], states[i], cities[i], streets[i])
        print 'created address %s' % address

def init_shop():
    # create categories for DessertItem
    create_category('Cakes')
    create_category('Cookies')
    create_category('Miscellaneous')

    LEN_CAT = 3
    PIC_DIR = 'dessert-pics'

    try:
        pics = os.listdir(PIC_DIR)
    except:
        print 'error opening %s, quitting' % PIC_DIR
        exit()

    names = (
        'Malaysia Shangri La Buffet',
        'Traditional Australian Christmas Dessert',
        'Medieval/Renaissance Tray',
        'Beckta Sunshine Cookie',
        'Strawberry and Raspberry Trifle',
        'Baked Chocolate Cupcake Cute Dess',
        'Mascarpone Delicious Master',
        'Indian Dessert Cookie',
        'Candy Chiristmas Cookie',
        'Chocolate Chip Cheesecake',
        'Festive Dessert Pizza',
        'Elegant Strawberry Birthday Cake'
    )
    details = (
        'Made by first-class dessert chef with guaranteed luscious satisfaction!',
        'Received exclamations from connoisseurs. A must try!',
        'Baked by Dessert House\'s ace dessert chef, purely Franch style.',
        'Latest experimental to the next-generation dessert, the future is for you to evaluate.'
        'No marketing hype. Superb quality and superb price, you have both!',
    )
    LEN_DTL = len(details)
    for i in range(len(pics)):
        name = names[i]
        detail = details[random.randint(0, LEN_DTL-1)]
        pic_path = os.path.join(PIC_DIR, pics[i])
        cat = Category.objects.get(pk=(random.randint(1, LEN_CAT)))

        di = create_dessert(
            pic_path,
            name = name,
            detail = detail,
            category = cat,
        )

        print 'created dessert: %s' % di

def init_backend():
    u = create_user('peter', 'peter@example.com', '123123')
    s = Staff.objects.create(user=u)
    print 'created staff: %s' % s
